﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// Authors Patrick Boos, Michael Gfeller, Silvan Gehrig
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Data;
using ScrumTable.BL.DM.DataManagement;
using Microsoft.Practices.Unity;
using ScrumTable.Config.ElementManagement;
using ScrumTable.UI.ViewModel.Data;
using ScrumTable.Config.ConfigManagement;
using ScrumTable.UI.ViewModel.Extension;

#endregion

namespace ScrumTable.UI.ViewModel.View
{
    /// <summary>
    /// Description of the class
    /// </summary>
    public class StackedDailyScrumStoryBoardViewModel : ViewModelBase
    {

        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        private ScrumViewModelData _scrumData;
        private ObservableCollection<UserStoryViewModel> _userStoriesViewModel;
        private TargetConfig _targetConfig;


        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------  

        /// <summary>
        /// Gets the user stories for must priority.
        /// </summary>
        /// <value>The user stories for must priority.</value>
        public ObservableCollection<UserStoryViewModel> UserStoriesAsViewModel
        {
            get
            {

                var project = ScrumData.CurrentProject;
                if (project != null)
                {
                    
                    if (_userStoriesViewModel == null)
                    {
                        _userStoriesViewModel = SetUserStoriesOnList(ref _userStoriesViewModel);
                    }
                    else
                    {
                        _userStoriesViewModel= SetUserStoriesOnList(ref _userStoriesViewModel);
                    }
                }
                return _userStoriesViewModel;
            }
        }



        /// <summary>
        /// Config for this view model
        /// </summary>
        public TargetConfig Config
        {

            get { return _targetConfig; }
            set
            {
                _targetConfig = value;

            }
        }

        public IEnumerable<ScrumState> States
        {
            get { return ElementManager.GetStates(Config); }
        }

        /// <summary>
        /// Gets the task list as a view model list.
        /// </summary>
        /// <value>The tasks as view model.</value>
        public List<List<UserStoryViewModel>> UserStoriesAsMultipleList
        {
            get
            {

                var liste = new List<List<UserStoryViewModel>>();
                var stories = _scrumData.CurrentIteration.Stories;
                for (int i = 0; i < _scrumData.CurrentIteration.Stories.Count; )
                {
                    var pairListe = new List<UserStoryViewModel>();
                    for (int u = 0; u < 2; u++)
                    {
                        pairListe.Add(stories.Count > i ? new UserStoryViewModel(stories.ElementAt(i)) : null);
                        i++;
                    }
                    liste.Add(pairListe);
                }
                return liste;
            }
        }

        /// <summary>
        /// Gets or sets the config manager.
        /// </summary>
        /// <value>The config manager.</value>
        [Dependency]
        public ConfigManager ConfigManager { get; set; }

        /// <summary>
        /// Gets or sets the scrum data.
        /// </summary>
        /// <value>The scrum data.</value>
        [Dependency]
        public ScrumViewModelData ScrumData
        {
            get
            {
                return _scrumData;
            }
            set
            {                
                _scrumData = value;
                ScrumData.DataContext.CollectionChanged += OnDataContextCollectionChanged;
                ScrumData.DataContext.ElementChanged += OnDataContextElementChanged;
            }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected override void ReleaseManagedResources()
        {
            ScrumData.DataContext.ElementChanged -= OnDataContextElementChanged;
            base.ReleaseManagedResources();
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------


        /// <summary>
        /// Moves the items in the current list. 
        /// </summary>
        /// <param name="listUserStories"></param>
        /// <returns></returns>
        private ObservableCollection<UserStoryViewModel> SetUserStoriesOnList(ref ObservableCollection<UserStoryViewModel> listUserStories)
        {
            var items = from x in ScrumData.CurrentIteration.Stories select new UserStoryViewModel(x);
            var stories = new ObservableCollection<UserStoryViewModel>(items);
            if (listUserStories == null)
            {
                listUserStories = stories;

            }
            else
            {
                listUserStories.UnionToLeft(stories);
            }
            listUserStories.SortDomainItems();
            return listUserStories;
        }
    

        void OnDataContextElementChanged(object sender, PropertyChangedEventArgs e)
        {
            var args = e as DomainChangedEventArgs;

            if (args == null) return;
            if (args.ContainsType<UserStory>(false) && args.ContainsPropertyName("Ranking"))
            {
                if (args.ContainsType<Iteration>(true))
                {
                    SendPropertyChanged("UserStoriesAsViewModel");
                }
            }
        }


        private void OnDataContextCollectionChanged(object sender, DomainCollectionChangedEventArgs e)
        {
            SendPropertyChanged("UserStoriesAsViewModel");
        }

        #endregion
    }
}
